package monster

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type Monster struct {
	Name  string
	Age   int
	Skill string
}

func (this *Monster) Store() bool {
	// 序列化
	data, err := json.Marshal(this)
	// 保存文件
	if err != nil {
		fmt.Println("marshal error ", err)
		return false
	}
	filePath := "d:/monster.ser"
	err1 := ioutil.WriteFile(filePath, data, 06666)
	if err1 != nil {
		fmt.Println("WriteFile error ", err)
		return false
	}
	return true
}

func (this *Monster) ReStore() bool {
	// 读取文件
	filePath := "d:/monster.ser"
	data, err := ioutil.ReadFile(filePath)
	if err != nil {
		fmt.Println("ReadFile error ", err)
		return false
	}
	// 反序列化
	err1 := json.Unmarshal(data, this)
	if err1 != nil {
		fmt.Println("Unmarshal error ", err)
		return false
	}
	return true
}
